﻿
Imports elpmis
Imports lpmsengi
Imports avacom
Imports xmlctrl

Public MustInherit Class cacheProcessor
    Inherits Global.cacheProcessor

    Protected MustOverride Function cookieName() As String

    Protected NotOverridable Overrides Function normalRetiredTicks() As Int64
        Return secondsToTicks(cacheSeconds >> 1)
    End Function

    Protected Overridable Function uselpmsengi() As Boolean
        Return False
    End Function

    Protected Overridable Function lpmsname() As String
        assert(False)
        Return Nothing
    End Function

    Private Delegate Function processDelegate(ByVal context As httpContext,
                                              ByVal id As String,
                                              ByVal value As String,
                                              ByVal format As String) As Boolean

    Protected Overridable Overloads Function process(ByVal context As httpContext,
                                                     ByVal id As String,
                                                     ByVal value As String,
                                                     ByVal format As String) As Boolean
        assert(False)
        Return False
    End Function

    Private Shared Function _processControl(ByVal context As httpContext,
                                            ByVal id As String,
                                            ByVal value As String,
                                            ByVal format As String,
                                            ByVal lpmsname As String) As xmlcontrol
        lpmsctx.insertThreadStaticVariable("id", typeDefination.string, id)
        lpmsctx.insertThreadStaticVariable("value", typeDefination.string, value)
        lpmsctx.insertThreadStaticVariable("format", typeDefination.string, format)
        Return context.createControl(New nameLpmsControl(lpmsname))
    End Function

    Protected Shared Function processControl(ByVal context As httpContext,
                                             ByVal id As String,
                                             ByVal format As String,
                                             ByVal cookiename As String,
                                             ByVal lpmsname As String) As xmlcontrol
        Dim value As String = Nothing
        preprocess(context, id, cookiename, value, Nothing)
        Return _processControl(context, id, value, format, lpmsname)
    End Function

    Protected Shared Function processControl(ByVal context As httpContext,
                                             ByVal id As String,
                                             ByVal cookiename As String,
                                             ByVal lpmsname As String) As xmlcontrol
        Dim value As String = Nothing
        Dim format As String = Nothing
        preprocess(context, id, cookiename, value, format)
        Return _processControl(context, id, value, format, lpmsname)
    End Function

    Private Shared Function processOutput(ByVal context As httpContext,
                                          ByVal id As String,
                                          ByVal cookiename As String,
                                          ByVal process As processDelegate) As Boolean
        assert(Not process Is Nothing)
        Dim value As String = Nothing
        Dim format As String = Nothing
        preprocess(context, id, cookiename, value, format)
        Return process(context, id, value, format)
    End Function

    Private Shared Sub preprocess(ByVal context As httpContext, ByVal id As String, ByVal cookiename As String,
                                  ByRef value As String, ByRef format As String)
        If Not isemptystring(id) Then
            userSharedCookie(id, cookiename, value)
        End If
        format = context.url().query(query.format)
    End Sub

#If RETIRED Then
    Protected Overridable Overloads Sub addresponseHeaders(ByVal context As httpContext,
                                                           ByVal value As String,
                                                           ByVal format As String)
        assert(False)
    End Sub

    Protected NotOverridable Overrides Sub addresponseHeaders(ByVal context As httpContext)
        If uselpmsengi() Then
            MyBase.addresponseHeaders(context)
        Else
            Dim id As String = Nothing
            context.userInput(query.id, id)
            Dim value As String = Nothing
            Dim format As String = Nothing
            preprocess(context, id, cookieName(), value, format)
            addresponseHeaders(context, value, format)
        End If
    End Sub
#End If

    Protected NotOverridable Overrides Function processImpl(ByVal context As httpContext) As Boolean
        Dim id As String = Nothing
        context.userInput(query.id, id)
        If uselpmsengi() Then
            Return processControl(context, id, cookieName(), lpmsname()).render()
        Else
            Return processOutput(context, id, cookieName(), AddressOf process)
        End If
    End Function
End Class
